// 资源请求
import axios from 'axios'
import Qs from 'qs'

// axios.defaults.withCredentials = true
axios.defaults.timeout = 10 * 1000
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8'
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'

axios.interceptors.response.use((response) => {
  // console.log(response)
  if (response.status !== 200) {
    // 接口错误处理
  }
  return response.data
}, (error) => {
  if (error.message.match('timeout')) {
    error.message = '网路超时，请检查您网络状态'
  }
  return Promise.reject(error)
})

// 后端接口 base 路径
const baseUrl = 'https://jsfz.meetingcn.org/'

const createApiHandler = (type = 'get', data) => {
  return (url, data, key) => {
    url = `${baseUrl}${url}`
    let keys = key
    if (type === 'get') {
      return axios.get(url, {
        params: data,
        headers: {
          'accesskey': keys,
          'Content-Type': 'application/x-www-form-urlencoded'
        }
      })
    } else if (type === 'post') {
      return axios.post(url, 
        data, {
        headers: {
          'accesskey': key,
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        transformRequest: [function (data) {
          data = Qs.stringify(data)  // 序列化参数
          return data
        }]
      })
    }
  }
}

const get = createApiHandler()
const post = createApiHandler('post')

const apis = {
  // 获得accesskey
  // getAcKey (data){
  //   return post('campus/key.php',data)
  // },
  // 登录
  getGrades (data, key) {
    return post('jwxt/get_data.json', data, key)
  },
  // 成绩查询的验证码,无需传key
  getCaptcha () {
    return get('jwxt/get_ready.json')
  },
  // 流量查询的验证码，无需传key
  getCaptcha1 () {
    return get('zfw/get_ready.json')
  },
  getFlow (data, key) {
    return post('zfw/get_data.json', data, key)
  },

  getAcademicList (data, key) {  //获取学术文章列表
    return get('campus/meeting.php', "", key)
  },
  getArticle (data, key) {   //获取学术文章内容
    return get('campus/meetdetail.php?id=' + data, "", key)
  },
  searchSchedule (data, key) {   //搜索课表
    return get('campus/kbsearch.php?mes=' + data, "", key)
  },
  getClass (data, key) {      //获得班级列表
    return post('campus/class.php', data, key)
  },
  getJobList1 (data, key) {
    return post('campus/job.php?area=north', data, key)
  },
  getJobList2 (data, key) {
    return post('campus/job.php?area=sourth', data, key)
  },
  getJobArticle (data, key) {
    return get('campus/jobdetail.php?aid=' + data, "", key)
  },
  getCarList (data, key) {
    return get('campus/infor.php?type=time', "", key)
  },
  searchCarList (data){
    return get('campus/infor.php?type=time&search='+data)
  },
  searchPhone (data){
    return get('campus/tel.php?search='+data)
  },

  // 工单
  getOrderList (data) {
    return get('/Index/index', data)
  },
  // 工单详情
  getOrderDetail (data) {
    return get('/Index/view', data)
  }
}

export default apis
